package com.wmyg.antifake.controller;

import com.github.pagehelper.PageInfo;
import com.wmyg.antifake.bean.*;
import com.wmyg.antifake.service.FactoryDeviceService;
import com.wmyg.antifake.service.FactoryService;
import com.wmyg.antifake.utils.FileUtil;
import com.wmyg.antifake.utils.ReplayUtils;
import com.wmyg.antifake.utils.ResultCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/factoryDevice")
public class FactoryDeviceController {

    @Autowired
    FactoryDeviceService factoryDeviceService;

    @Autowired
    FactoryService factoryService;

    @Autowired
    RedisTemplate redisTemplate;

    @Value("${files.uploadFolder}")
    private String uploadBasePath;
    @Value("${files.staticAccessPath}")
    private String staticAccessPath;

    @RequestMapping(method = RequestMethod.GET,value = "deviceIndex")
    public ModelAndView deviceIndex(ModelAndView mv){
        mv.setViewName("factory/factoryDeviceList");
        mv.addObject("staticAccessPath",staticAccessPath);
        return mv;
    }

    @RequestMapping(value = "deviceList")
    public ReplayUtils deviceList(ModelAndView mv, Factory factory) {
        PageInfo<Factory> pageInfo =  factoryService.selectFindAll(factory);
        return new ReplayUtils(pageInfo.getTotal(), pageInfo.getList(),0);
    }

    @RequestMapping(value = "bindDevice")
    public String  bindDevice(FactoryDevice factoryDevice){
        return ResultCodeEnum.getResponseMsg(factoryDeviceService.insertFactoryDevice(factoryDevice));
    }


    @RequestMapping(method = RequestMethod.GET,value = "deviceInfoIndex")
    public ModelAndView deviceInfoIndex(ModelAndView mv, Integer factoryId, HttpSession s){
        s.setAttribute("factoryId",factoryId);
        mv.addObject("factoryId",factoryId);
        mv.setViewName("factory/deviceInfo");
        return mv;
    }

    @RequestMapping(value = "deviceInfo")
    public ReplayUtils deviceInfo(FactoryDevice factoryDevice,HttpSession s){
        //PageInfo<FactoryDevice> pageInfo = factoryDeviceService.selectDeviceByFactoryId((Integer) s.getAttribute("factoryId"));
        PageInfo<FactoryDevice> pageInfo = factoryDeviceService.selectDeviceByFactoryId(factoryDevice);
        for(FactoryDevice list:pageInfo.getList()){
            if(redisTemplate.opsForValue().get(list.getDeviceId())!=null){
                list.setIdentifyCode(redisTemplate.opsForValue().get(list.getDeviceId()).toString());
            }
        }
        return new ReplayUtils(pageInfo.getTotal(), pageInfo.getList(),0);
    }

    @RequestMapping(method = RequestMethod.GET,value = "/toModifyFactory")
    public ModelAndView toModifyFactory(ModelAndView mv,String factoryId){
        if(StringUtils.isNotEmpty(factoryId)){
            Factory factory=factoryService.selectById(Integer.parseInt(factoryId));
            mv.addObject("factory",factory);
            mv.addObject("staticAccessPath",staticAccessPath);
        }else{
            mv.addObject("factory",new Factory());
        }
        mv.setViewName("factory/addFactory");
        return mv;
    }

    /**
     * 上传图片
     * @param requests
     * @param response
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> upload(MultipartHttpServletRequest requests, HttpServletResponse response){
        //上传路径
        String filePath =  "/static/pic/factory/";
        Map<String,Object> baseResult = FileUtil.upload(requests,filePath,staticAccessPath,uploadBasePath);
        return baseResult;
    }

    /**
     * 添加或修改
     * @param factory
     * @return
     */
    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    public ModelAndView insertOrUpdate(Factory factory,ModelAndView mv){
        int result = factoryService.insertOrUpdate(factory);
        mv.setViewName("factory/factoryDeviceList");
        return mv;
    }

    /**
     * 删除
     * @param factoryId
     * @return
     */
    @RequestMapping(value = "/deleteFactory", method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteFactory(String factoryId){
        if(StringUtils.isEmpty(factoryId)){
            return 0;
        }
        int result = factoryService.delete(NumberUtils.toInt(factoryId));
        return result;
    }
}
